<%@page import="fr.uniscol.model.BaseAnneeScolaire"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<script type="text/javascript">
<!--
	$(document).ready(function(){
		$("#btnEnregistrer").click(saveClasse);
	});

	function editClasse(classeId){
		/*$("div.modal-body").load("classe.edit", {"classeid":classeId},  function(){
			$("#classeFormModal").modal("show");
		});*/
		
		location = 'classe.edit?classeid=' + classeId;
	}
	
	function saveClasse(){
		$.ajax({url:$("form[name='frmClasse']").attr("action"),
			   type: "POST",
			   data: $("form[name='frmClasse']").serialize(),
			   success : function(data){
				   if(data.code == 0){
					   alert(data.message);
				   	   location.reload();
				   	   
				   }else{
					   alert(data.message);
				   }
			   },
			   error : function(){
				   alert("ECHEC: Impossible de joindre le serveur !");
			   },
			   dataType:"json"}
		);
	}
    
    function deleteClasse(classeId, libelle){
        
        $(".modal-body").html("Supprimer la classe " + libelle + " de la base ?");
        
        $("#btnEnregistrer").html("Supprimer");
        $("#btnEnregistrer").click(function(){
           location = 'classe.delete?classeid=' + classeId; 
        });
        
        $("#classeFormModal").modal("show");
    }
//-->
</script>

<div class="container-fluid">
	<div class="row">
		<div class="col-md-10">
			<h4>Classes</h4>
			<table class="table table-striped table-hover">
				<thead>
					<tr>
						<th>Code</th>
						<th>Libellé</th>
						<th>Structure SIECLE</th>
						<th>Effectif</th>
						<th></th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${classes}" var="classe">
					<tr>
						<td>${classe.id}</td>
						<td>${classe.libelle}</td>
						<td>${classe.codeStructure}</td>
						<td>${classe.nbEleves}</td>
						<td class="buttons">
							<button type="button" class="btn btn-primary btn-xs" onClick="javascript:editClasse(${classe.id})">Modifier</button>
							<button type="button" class="btn btn-default btn-xs" onClick="javascript:deleteClasse(${classe.id}, '${classe.libelle}')">Supprimer</button>
						</td>
					</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
	</div>
</div>

<div id="classeFormModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Classe</h4>
      </div>
      <div class="modal-body">

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
        <button id="btnEnregistrer" type="submit" class="btn btn-primary">Enregistrer</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
